$(function () {

  var options = {
    url: api + "/scabpm/bpm/carbonCopy/receiveList",
    listType: 'bpm',
    updateUrl: '/pages/bpm/copy/edit',
    method: 'post',
    modalName: "发起申请",
    columns: [
      {
        checkbox: true
      },
      {
        field: 'subject',
        title: '标题',
      },
      {
        field: 'nodeName',
        title: '抄送节点'
      },
      {
        field: 'triggerUserName',
        title: '抄送人'
      },
      {
        field: 'status',
        title: '状态',
        align: 'center',
        formatter: function (value, row, index) {
          return statusDict(value)
        }
      }
      , {
        field: 'receiveTime',
        title: '抄送时间',
      },{
        title: '操作',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '流程任务表单【'+row.subject+'】-【'+row.nodeName+'】'
          var url = options.updateUrl + '?id='+row.id+'&nodeId='+row.nodeId+'&instId='+row.instId
          actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="openDetail(\'' + row.id + '\',\'' + name + '\', \'' + url + '\')"><i class="fa fa-eye"></i>查看任务详情</a> ');
          actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="msgWarn(\'' + row.recordId + '\', \'' + row.subject + '\')"><i class="fa fa-comments"></i>提醒消息内容</a> ');
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

function statusDict(item){
  var dict = {
    'read': {text:'已读',color:'label-info'},
    'unread': {text:'未读',color:'label-danger'},
    'reviewed': {text:'已审阅',color:'label-warning'}
  }
  return '<span class="label '+(dict[item] ? dict[item].color : '')+' list-label">'+(dict[item] ? dict[item].text : item)+'</span>'
}

function openDetail(id, name, url){
  $.operate.post(api+'/scabpm/bpm/carbonCopy/updateRead',{id: id},function (res) {
    if (res.isOk){
      $.table.refresh()
    }
  },false)

  $.operate.editFullBpm(id, name, url)
}

function markRead() {
  var checkRows = $('#bootstrap-table').bootstrapTable('getSelections')
  if (!checkRows.length){
    $.modal.msgWarning('请至少选择一条数据')
    return false
  }
  var ids = []
  checkRows.forEach(function(row){
    ids.push(row.id)
  })
  $.modal.confirm("确认将选中记录更新为已读吗？" , function () {
    $.operate.post(api+'/scabpm/bpm/carbonCopy/updateRead',{id: ids.join(',')},function (res) {
      if (res.isOk){
        $.table.refresh()
        $.modal.msgSuccess(res.msg == null ? '更新成功' : res.msg);
      }else{
        $.modal.alertError(res.msg ? res.msg : '错误！');
      }

    },false)
  });
}

//提醒消息内容
function msgWarn(id, name) {
	$.operate.post(api+'/scabpm/bpm/carbonCopy/updateRead',{id: id},function (res) {
	  if (res.isOk){
	    $.table.refresh()
	  }
	},false)
  $.modal.openview('【'+name+'】', '/pages/bpm/copy/msg?id='+id, '600','400')
}